java API

Posted by ZhengYang on 2016-09-23

Interface

  • java.lang.Iterable
    • java.util.Collection
      • java.util.List 对象间有顺序,允许重复
      • java.util.Set 对象间无顺序,不允许重复
  • java.util.Map
    • java.util.SortedMap

Class

  • java.util.AbstractCollection
    • java.util.AbstractList
      • java.util.AbstractSequentialList
        • java.util.LinkedList
      • java.util.ArrayList
      • java.util.Vector
        • java.Stack
    • java.util.AbstractSet
      • java.util.HashSet
        • java.LinkedHashSet
      • java.util.TreeSet
  • java.util.AbstractMap
    • java.util.HashMap
      • java.util.LinkedHashMap
    • java.util.TreeMap
  • java.util.Dictionary
    • java.util.HashTable

1 正则表达式 字符串

1.1 字符串常量 字符串变量

String 字符串常量 不可变,所以线程安全
StringBuffer 字符串变量 线程安全
StringBuilder 字符串变量 线程不安全

1.2 正则表达式

模式 解释
()
* 任意次
+ 一次或多次
? 零次或一次
\d{n} 数字匹配n次
\d{n,} 数字至少匹配n次
\d{n,m} 数字匹配n-m次
. 除\r\n之外的任意字符
[\s\S] 任意字符,包括\r\n
x|y 匹配x或y
[xy] 匹配x或y
[^xy] 不匹配x或y
[A-Za-z0-9] 匹配所有字母和数字
\d 数字
\D 非 \d
\s 不可见字符(空格,tab,换行)
\S 非 \s
\w 包括下划线的任何字符
\W 非 \w

1.3 正则匹配 字符串的基本函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Regex {
public static void main(String[] args) {
//// 字符串正则匹配
// 字符串 和 匹配模式
String text = "123456789-a12354-54-";
String pattern = "(\\d*)-";
// 获得 matcher对象
Matcher matcher = Pattern.compile(pattern).matcher(text);
// 有多少个组(pattern中的小括号数),group()默认为0,表示正常全匹配
System.out.println(matcher.groupCount());
// 输出匹配的结果
while(matcher.find()){
System.out.println(matcher.group());
System.out.println(matcher.group(1));
}
//// 字符串划分,字符串替换
String str = "ab,cd, dsg.";
String[] fields = str.replace(".", "").split("\\s*,\\s*");
for(int i=0; i<fields.length; ++i)
System.out.println(fields[i]);
//// 字符串操作
String s1 = "123ajioin145a";
String s2 = "321aniiia";
// 字符串比较
s1.equals(s2);
// 忽略大小写比较
s1.equalsIgnoreCase(s2);
// 返回index=3的字符
s1.charAt(3);
// 返回字母a在字符创s1中,第一次出现的位置,从0位置开始找
System.out.println(s1.indexOf("a"));
// 区别是,从5位置开始找
System.out.println(s1.indexOf('a',5));
//截取s1第6个字符到末尾的字符串
s1.substring(6);
//截取s1第6个字符到第8个字符,不包括第8,类似python
s1.substring(6,8);
}
}

1.4 String变为int的三种方法

1
2
3
int a1 = Integer.parseInt(s1);
int a2 = new Integer(s1).intValue();
int a3 = Integer.valueOf(s3).intValue()

2 基本类型 包装类型

基本类型 包装类 字符串str -> 基本类型 包装类 -> 基本类型
Xxx包装类.parseXxx(str) Xxx包装类对象.xxxValue()
整型 byte Byte
short Short
int Integer Integer.parseInt(str) New Integer(s1).intValue()
long Long
浮点型 float Float
double Double
char型 char Character
boolean型 boolean Boolean Boolean.getBoolean(str)

3 ArraySort

java中已经有写好的排序算法,只需调用Arrays.sort()即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.util.*;
public class ArraySort {
public static void main(String[] args) {
//// int数组只能升序排序
int[] a = {4,3,1};
Arrays.sort(a);
// 输出数组
for(int i=0; i<a.length; i++)
System.out.print(a[i]+" ");
System.out.println();
//// Integer数组排序,可以顺序排,也可以逆序排
Integer[] b = {4,3,1};
// 逆序比较器
Arrays.sort(b);
// 逆序1
Arrays.sort(b, Collections.reverseOrder());
// 逆序2 自己构造比较器
Comparator<Integer> cmp1 = new Comparator<Integer>(){
public int compare(Integer i, Integer j){
return -i.compareTo(j);
}
};
Arrays.sort(b, cmp1);
//// String数组排序,可以顺序,也可以逆序
String[] strArr = {"a","b","A"};
// 字典顺序
Arrays.sort(strArr);
// 字典逆序1
Arrays.sort(strArr,Collections.reverseOrder());
// 字典逆序2 自己构造比较器
Comparator<String> cmp2 = new Comparator<String>(){
public int compare(String i, String j){
return -i.compareTo(j);
}
};
Arrays.sort(strArr,cmp2);
// 输出数组
for(int i=0; i<strArr.length; i++)
System.out.print(strArr[i]+" ");
System.out.println();
}
}

4 List

4.1 ArrayList LinkedList的基本函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.util.*;
public class List{
public static void main(String[] args) {
// 有序,底层为数组
ArrayList<Integer> list1 = new ArrayList<Integer>();
// 有序,底层为链表
LinkedList<Integer> list2 = new LinkedList<Integer>();
//// 以下方法,ArrayList和LinkedList都适用
// add(value)
list1.add(4); list1.add(3); list1.add(1);list1.add(5);
// remove(index)
list1.remove(3);
// list1的最大值
Collections.max(list1);
// list1的最小值
Collections.min(list1);
// list1中的3替换为30
Collections.replaceAll(list1, 3, 30);
// list1的clone
ArrayList<Integer> list1Clone = (ArrayList<Integer>) list1.clone();
// list1的swap
Collections.swap(list1, 0, 2);
// list1的全填充
Collections.fill(list1, 100);
// list1的排序
Collections.sort(list1);
// list1的逆序
Collections.reverse(list1);
// list1 向右旋转1个单位
Collections.rotate(list1, 1);
// 输出ArrayList
System.out.println(list1.toString()); //中括号[100, 100, 100]
//// 遍历
// ArrayList 适合1
int size = list1.size();
for(int i=0; i<size; ++i)
System.out.println(list1.get(i));
// ArrayList 适合2
for(int i=0; i<list1.size(); ++i)
System.out.println(list1.get(i));
// LinkedList 非常适合1
Iterator<Integer> it = list2.iterator();
while(it.hasNext())
System.out.println(it.next());
// LinkedList 非常适合2
for(Integer i: list2)
System.out.println(i);
}
}

4.2 Vector ArrayList

Vector ArrayList
synchronized否 同步 非同步

5 Set

5.1 HashSet LinkedHashSet TreeSet

HashSet LinkedHashSet TreeSet
实现 hash表 hash表+双链表 红黑树
基本操作(add,remove,contains,size)时间复杂度 O(1) O(1) O(log(n))
元素有序否 无序 插入序 升序
多线程同步否 不同步 不同步 不同步
允许插入null否 允许,但多次add(null),也只能放入一个null 不允许 允许,但多次add(null),也只能放入一个null

在每个覆盖了equals方法的类中,也必须覆盖hashCode方法。
hashcode在集合中会用到,将一个对象放入集合的流程:

  1. 判断该对象的hashcode与集合中的任意对象的hashcode是否相等
  2. 如果不等,放入该对象;如果相等,比较equals()是否相等
  3. 如果不等,放入该元素;如果相等,不放入集合

5.2 Set的基本函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.util.*;
public class Set {
public static void main(String[] args) {
// 无序,无重复,可以add(null) // 遍历速度中等,无论哪种遍历方法
HashSet<Integer> set1 = new HashSet<Integer>();
// 无序,无重复,可以add(null) // 遍历速度最快,无论哪种遍历方法
LinkedHashSet<Integer> set2 = new LinkedHashSet<Integer>();
// 升序,无重复,不能add(null) // 遍历速度最慢,无论哪种遍历方法
TreeSet<Integer> set3 = new TreeSet<Integer>();
// add(value)
set1.add(4); set2.add(3); set3.add(3);set2.add(null);
// remove(value)
set2.remove(null);
// 如果两个集合无交集,则返回true
Collections.disjoint(set1, set3);
// 集合最大值
Collections.max(set1);
// 集合最小值
Collections.min(set1);
// 直接输出
System.out.println(set1.toString()); // 中括号[]
//// 迭代输出
// Set迭代输出1
for(Integer i: set3){
System.out.println(i);
}
// Set迭代输出2
Iterator<Integer> it1 = set3.iterator();
while(it1.hasNext()){
System.out.println(it1.next());
}
// TreeSet多一种的降序迭代输出
Iterator<Integer> it2 = set3.descendingIterator();
while(it2.hasNext())
System.out.println(it2.next());
}
}

6 Map

6.1 HashMap LinkedHashMap TreeMap 的基本函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import java.util.*;
import java.util.Map.Entry;
public class Map {
public static void main(String[] args) {
// key用hashset存储,value都是Collection
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
// key用hashset加linkedlist存储
LinkedHashMap<Integer, Integer> map2 = new LinkedHashMap<Integer, Integer>();
// key用红黑树有序存储
TreeMap<Integer, Integer> map3 = new TreeMap<Integer, Integer>();
//// 以下方法对于HashMap,LinkedHashMap,TreeMap都适用
// 添加key value
map.put(2, 20); map.put(1, 10); map.put(3, 30);
// 移除 key
map.remove(3);
// map的size(),这里不是用length
map.size();
// 判断是否存在key=1的Entry
map.containsKey(1);
// 返回key的Set
map.keySet();
// 返回values的Collection
map.values();
// 返回Set<Entry<Key, Value>>
map.entrySet();
// 替换key=1,对应的value值为100
map.replace(1, 100);
// AbstractMap的都可以equals比较
map2.put(2, 20); map2.put(1, 100);
// 只要key和value都相等就行,HashMap,LinkedHashMap,TreeSet都可以equals比较
System.out.println(map.equals(map2));
map3.put(2, 20); map3.put(1, 100);
// 只要key和value都相等就行,HashMap,LinkedHashMap,TreeSet都可以equals比较
System.out.println(map.equals(map3));
// 输出
System.out.println(map.toString()); //大括号{1=100, 2=20}
//// 迭代输出
// HashMap 迭代的三种方式
// 1 key和value单独遍历
for(int key: map.keySet()){ //Set
System.out.println(key);
}
for(int value: map.values()){ // Collection
System.out.println(value);
}
// 2 key和value一起得到
for(Entry<Integer, Integer> entry : map.entrySet()){ // Entry
System.out.println(entry);
}
// 3 迭代
Iterator<Integer> iter = map.keySet().iterator(); //Set
while(iter.hasNext()){
System.out.println(iter.next());
}
}
}

6.2 HashMap Hashtable

HashMap Hashtable
synchronized 否 非同步 同步
允许nul否 允许 不允许
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
Hashtable<Integer, Integer> tab = new Hashtable<Integer, Integer>();
map.put(2, 20);
map.put(1, 10);
map.put(3, 30);
tab.put(2, 20);
tab.put(1, 10);
tab.put(3, 30);
// HashMap 迭代的三种方式
// 1
for(int key: map.keySet()){ //Set
System.out.println(key);
}
for(int value: map.values()){ // Collection
System.out.println(value);
}
// 2
for(Entry<Integer, Integer> entry : map.entrySet()){ // Entry
System.out.println(entry);
}
// 3
Iterator iter = map.keySet().iterator();
while(iter.hasNext()){
System.out.println(iter.next());
}
// Hashtable 多一种迭代的方式
Enumeration e = tab.keys(); // Enumeration
while(e.hasMoreElements()){
System.out.println(e.nextElement());
}
Enumeration e1 = tab.elements(); // Enumeration
while(e1.hasMoreElements()){
System.out.println(e1.nextElement());
}

7 格式化

7.1 取2位小数

1
2
3
4
5
6
7
8
// 四舍五入 1.17
System.out.println(String.format("%.2f", 1.1675));
// 向上取小数 1.17
System.out.println(Math.ceil(1.1675*100)/100);
System.out.println(String.format("%.2f", Math.ceil(1.1675*100)/100));
// 向下取小数 1.16
System.out.println(Math.floor(1.1675*100)/100);
System.out.println(String.format("%.2f", Math.floor(1.1675*100)/100));

7.2 取整

1
2
3
4
5
6
// 四舍五入 12
System.out.println(Math.round(11.675)); // round返回的是long
// 向上取整 12
System.out.println((int)Math.ceil(11.675)); // ceil返回的是double
// 向下取整 11
System.out.println((int)Math.floor(11.675)); // floor返回的是double

8 java计时

1
2
3
4
5
6
7
8
9
long startMili=System.currentTimeMillis(); // 当前时间对应的毫秒数
System.out.println(startMili);
// 执行一段代码,求一百万次随机值
for(int i=0;i<1000000;i++){
Math.random(); // [0,1) double
}
long endMili=System.currentTimeMillis();
System.out.println(endMili);
System.out.println(endMili-startMili);

9 随机数

1
2
3
4
5
6
7
8
Math.random(); // [0,1) double
import java.util.Random;
Random r = new Random();
r.setSeed(0); // 设种子
r.nextInt(); // [-2147483648,2147483647] int
r.nextInt(10); // [0,9] int
r.nextInt(10)+1; // [1,10] int

10 Math类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Math.abs(-1.1); // 绝对值
Math.ceil(-1.1); // 顶函数 -1.0
Math.round(-1.1); // 四舍五入 - 1
Math.floor(-1.1); // 底函数 -2.0
// 都是以弧度计算的
Math.sin(1.57);
Math.cos(1);
Math.tan(1);
Math.toDegrees(Math.PI); // 180.0
Math.toRadians(180); // 3.141592653589793
double e = Math.E;
double p = Math.PI;
Math.exp(1); // e的1次
Math.pow(2, 5); // 2的5次
Math.log(1); // e为底,1的对数
Math.log10(1); // 10 为底,1的对数
double l = Math.log(5) / Math.log(2); // log以2为底,5的对数
Math.sqrt(1); // 开根号
Math.pow(8, 1.0/3); // 开3次方,注意必须1.0/3,否则1/3=0

Reference

  1. http://stackoverflow.com/questions/8962459/java-collections-keyset-vs-entryset-in-map
  2. http://stackoverflow.com/questions/1463284/hashset-vs-treeset
  3. http://www.cnblogs.com/Terry-greener/archive/2011/12/02/2271707.html
  4. http://blog.csdn.net/afgasdg/article/details/6889383
  5. http://www.importnew.com/8773.html
  6. http://www.programcreek.com/2013/03/hashset-vs-treeset-vs-linkedhashset/
  7. http://blog.csdn.net/rmn190/article/details/1492013
  8. http://www.cnblogs.com/interdrp/p/5004000.html